<?php

App::uses('AppController', 'Controller');

class CompanyController extends AppController {

    public $helpers = array('Html');

    public function view($id) {
        $this->loadModel('Broker');
        $this->loadModel('Company');
        $this->loadModel('Opinion');
        
        if (!$id) {
            throw new NotFoundException(__('Invalid post'));
        }
        $company = $this->Company->findById($id);
        if (!$company) {
            throw new NotFoundException(__('Invalid post'));
        }

        $this->paginate = array(
            'conditions' => array('status' => 1, 'company' => $company['Company']['id']),
            'limit' => 12,
            'order' => array('id' => 'desc')
        );

        $rows = $this->paginate('Broker');

        foreach ($rows as $pk => $performer) {
            $opinions = $this->Opinion->find('all', array('conditions' => array('broker' => $performer['Broker']['id'], 'result >' => 0)));
            $performer['Broker']['opinions'] = $opinions;
            $rows[$pk] = $performer;
        }

        $this->set('rows', $rows);
        $this->set('company', $company);
    }

}
